### Project 12 Ultrasonic Following Tank ![](media/image-20250908172315808.png) **Description** In project 11, we made an obstacle avoidance car. In fact, we only need to alter a test code to transform an obstacle avoidance car into a following car. In this lesson, we will make an ultrasonic following robot. The ultrasonic sensor detects the distance between smart car and the obstacle to drive tank car to move. **The specific logic of ultrasonic follow robot is as shown below:** | **Detection** | **Measured distance of front obstacles** | **Distance (unit: cm)** | | ------------- | ---------------------------------------- | ----------------------- | | Settings | Servo angle 90° | | | | 8X16 LED panel shows the icon “V” | | | If | 20≤ distance ≤60 | | | Status | Go front(set PWM to 200) | | | If | 10\= 20 && distance <= 60) //range to go front { Car_front(); } else if (distance > 10 && distance < 20) //range to stop { Car_Stop(); } else if (distance <= 10) //range to go back { Car_back(); } else //other situations, stop { Car_Stop(); } } /***********the function for motor running****************/ void Car_front() { digitalWrite(MR_Ctrl,LOW); analogWrite(MR_PWM,200); digitalWrite(ML_Ctrl,LOW); analogWrite(ML_PWM,200); } void Car_back() { digitalWrite(MR_Ctrl,HIGH); analogWrite(MR_PWM,200); digitalWrite(ML_Ctrl,HIGH); analogWrite(ML_PWM,200); } void Car_left() { digitalWrite(MR_Ctrl,LOW); analogWrite(MR_PWM,200); digitalWrite(ML_Ctrl,HIGH); analogWrite(ML_PWM,200); } void Car_right() { digitalWrite(MR_Ctrl,HIGH); analogWrite(MR_PWM,200); digitalWrite(ML_Ctrl,LOW); analogWrite(ML_PWM,200); } void Car_Stop() { digitalWrite(MR_Ctrl,LOW); analogWrite(MR_PWM,0); digitalWrite(ML_Ctrl,LOW); analogWrite(ML_PWM,0); } /******************dot matrix********************/ // the function for dot matrix display void matrix_display(unsigned char matrix_value[]) { IIC_start(); // call the function that data transmission start IIC_send(0xc0); //Choose address for(int i = 0;i < 16;i++) //pattern data has 16 bits { IIC_send(matrix_value[i]); //data to convey patterns } IIC_end(); //end to convey data pattern IIC_start(); IIC_send(0x8A); //select pulse width4/16, control display IIC_end(); } //The condition starting to transmit data void IIC_start() { digitalWrite(SCL_Pin,HIGH); delayMicroseconds(3); digitalWrite(SDA_Pin,HIGH); delayMicroseconds(3); digitalWrite(SDA_Pin,LOW); delayMicroseconds(3); } // transmit data void IIC_send(unsigned char send_data) { for(char i = 0;i < 8;i++) //Each byte has 8 bits { digitalWrite(SCL_Pin,LOW); //pull down clock pin SCL Pin to change the signals of SDA delayMicroseconds(3); if(send_data & 0x01) //set high and low level of SDA_Pin according to 1 or 0 of every bit { digitalWrite(SDA_Pin,HIGH); } else { digitalWrite(SDA_Pin,LOW); } delayMicroseconds(3); digitalWrite(SCL_Pin,HIGH); //pull up clock pin SCL_Pin to stop transmitting data delayMicroseconds(3); send_data = send_data >> 1; // detect bit by bit, so move the data right by one } } //The sign that data transmission ends void IIC_end() { digitalWrite(SCL_Pin,LOW); delayMicroseconds(3); digitalWrite(SDA_Pin,LOW); delayMicroseconds(3); digitalWrite(SCL_Pin,HIGH); delayMicroseconds(3); digitalWrite(SDA_Pin,HIGH); delayMicroseconds(3); } /***************end dot matrix display******************/ //The function to control servo void procedure(int myangle) { for (int i = 0; i <= 50; i = i + (1)) { pulsewidth = myangle * 11 + 500; digitalWrite(servoPin,HIGH); delayMicroseconds(pulsewidth); digitalWrite(servoPin,LOW); delay((20 - pulsewidth / 1000)); }} //The function to control ultrasonic sensor function controlling ultrasonic float checkdistance() { digitalWrite(Trig, LOW); delayMicroseconds(2); digitalWrite(Trig, HIGH); delayMicroseconds(10); digitalWrite(Trig, LOW); float distance = pulseIn(Echo, HIGH) / 58.20; //58.20, that is , 2*29.1=58.2 delay(10); return distance; } //**************************************************************** ``` **Test Result** Upload code successfully, DIP switch is dialed to the right end, the servo rotates to 90°, “V” is shown on 8X16 LED panel and smart car moves as the obstacle moves.